package com.amoby.base.commbank.commbiz;

import java.util.Date;

import com.amoby.base.utils.DateUtil;
import com.amoby.base.utils.DateUtils;
import com.amoby.base.utils.StringUtil;

public class HeaderRecord {
    // 固定填充的字符串
    private static final String RECORD_TYPE          = "0";
    private static final String BLANK_17             = StringUtil.leftPad("", 17); // 17个空格
    private static final String REEL_SEQUENCE_NUMBER = "01";
    private static final String NAME_OF_USER_BANK    = "CBA";
    private static final String BLANK_7              = StringUtil.leftPad("", 7); // 7个空格
    private static final String BLANK_40             = StringUtil.leftPad("", 40); // 40个空格
    /** Number of User Supplying File 账号ID */
    private Integer             commBizNumber;
    /** Name of User Supplying File 首选名称 */
    private String              commBizName;
    /** Description of Entries on File (e.g. salaries) 描述 */
    private String              description;
    /** Date to be Processed 交易日期（用于利息结算） */
    private Date                date;

    public static void main(String[] args) {
        String s = null;
        int i = Integer.valueOf(s);
        System.out.println(i);
    }

    /**
     * 将信息序列化为字符串
     * @return 序列化结果
     * @throws IllegalArgumentException 信息有误
     */
    public String serialize() throws IllegalArgumentException {
        StringBuffer sb = new StringBuffer();
        // 序列化常量字符
        sb.append(RECORD_TYPE).append(BLANK_17).append(REEL_SEQUENCE_NUMBER).append(NAME_OF_USER_BANK).append(BLANK_7);
        // 序列化数据字段
        sb.append(StringUtil.leftJustified(commBizName, 26));
        sb.append(StringUtil.rightJustified(commBizNumber.toString(), 6, "0"));
        sb.append(StringUtil.leftJustified(description, 12));
        // 验证交易日期（最早不得早于5天前，最晚不得晚于420天后）
        Date today = new Date();
        if (DateUtil.dateDiff(date, today, DateUtil.DIFF_DAY) > 5) {
            throw new IllegalArgumentException("交易时间过早 " + date);
        }
        if (DateUtil.dateDiff(today, date, DateUtil.DIFF_DAY) > 420) {
            throw new IllegalArgumentException("交易时间过晚 " + date);
        }
        sb.append(DateUtils.format(date, "ddMMyy"));
        // 序列化常量字符
        sb.append(BLANK_40);
        return sb.toString();
    }

    public Integer getCommBizNumber() {
        return this.commBizNumber;
    }

    public String getCommBizName() {
        return this.commBizName;
    }

    public void setCommBizNumber(Integer commBizNumber) {
        this.commBizNumber = commBizNumber;
    }

    public void setCommBizName(String commBizName) {
        this.commBizName = commBizName;
    }

    public String getDescription() {
        return this.description;
    }

    public Date getDate() {
        return this.date;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setDate(Date date) {
        this.date = date;
    }
}
